1 using UnityEngine;
2 using
System.Collections;
3 using
System.Collections.Generic;
4
5
6 ///
<summary>
7 ///
nodes should be in the order start, control, end
8 ///
</summary>
9 public
class GoSplineQuadraticBezierSolver : AbstractGoSplineSolver
10 {
11     
public GoSplineQuadraticBezierSolver( List<Vector3> nodes )
12     {
13         _nodes = nodes;
14     }
15
16     
17     
// http://www.gamedev.net/topic/551455-length-of-a-generalized-quadratic-bezier-curve-in-3d/
18     
protected float quadBezierLength( Vector3 startPoint, Vector3 controlPoint, Vector3 endPoint )
19     {
20         
// ASSERT: all inputs are distinct points.
21         
var A = new Vector3[2];
22         A[
0] = controlPoint - startPoint;
23         A[
1] = startPoint - 2f * controlPoint + endPoint;
24     
25         
float length;
26     
27         
if( A[1] != Vector3.zero )
28         {
29             
// Coefficients of f(t) = c*t^2 + b*t + a.
30             
float c = 4.0f * Vector3.Dot( A[1], A[1] ); // A[1].Dot(A[1]); // c > 0 to be in this block of code
31             
float b = 8.0f * Vector3.Dot( A[0], A[1] ); // A[0].Dot(A[1]);
32             
float a = 4.0f * Vector3.Dot( A[0], A[0] ); // A[0].Dot(A[0]); // a > 0 by assumption
33             
float q = 4.0f * a * c - b * b; // = 16*|Cross(A0,A1)| >= 0
34     
35             
float twoCpB = 2.0f * c + b;
36             
float sumCBA = c + b + a;
37             
float mult0 = 0.25f / c;
38             
float mult1 = q / ( 8.0f * Mathf.Pow( c, 1.5f ) );
39             length = mult0 * ( twoCpB * Mathf.Sqrt( sumCBA ) - b * Mathf.Sqrt( a ) ) +
40                 mult1 * ( Mathf.Log(
2.0f * Mathf.Sqrt( c * sumCBA ) + twoCpB ) - Mathf.Log( 2.0f * Mathf.Sqrt( c * a ) + b ) );
41         }
42         
else
43         {
44             length =
2.0f * A[0].magnitude;
45         }
46     
47         
return length;
48     }
49
50     
51     
#region AbstractGoSplineSolver
52     
53     
public override void closePath()
54     {
55         
56     }
57     
58     
59     
public override Vector3 getPoint( float t )
60     {
61         
float d = 1f - t;
62         
return d * d * _nodes[0] + 2f * d * t * _nodes[1] + t * t * _nodes[2];
63     }
64
65     
66     
public override void drawGizmos()
67     {
68         
// draw the control points
69         
var originalColor = Gizmos.color;
70         Gizmos.color = Color.red;
71         
72         Gizmos.DrawLine( _nodes[
0], _nodes[1] );
73         Gizmos.DrawLine( _nodes[
1], _nodes[2] );
74         
75         Gizmos.color = originalColor;
76     }
77     
78     
#endregion
79
80 }



Trò chơi Angry Birds trong UNITY Engine 31.665 lượt xem

Gõ tìm kiếm nhanh...